package meo;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import javax.xml.stream.XMLStreamWriter;

/**
 *
 * @author phamthanh
 */
public final class NorPunc {

    public NorPunc(String inF, String ouF) throws XMLStreamException, FileNotFoundException, IOException {
        long startTime = System.currentTimeMillis();

        FileInputStream fis = new FileInputStream(inF);
        XMLStreamReader xmlStreamReader = XMLInputFactory.newInstance().createXMLStreamReader(fis);
        FileOutputStream fos = new FileOutputStream(ouF);
        OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF8");
        XMLStreamWriter xmlStreamWriter = XMLOutputFactory.newInstance().createXMLStreamWriter(osw);
        xmlStreamWriter.writeStartElement("root");
        while (xmlStreamReader.hasNext()) {
            int eventCode = xmlStreamReader.next();
            switch (eventCode) {

                case XMLStreamConstants.START_ELEMENT:
                    String localName = xmlStreamReader.getLocalName();
                    if (localName.equals("record")) {
                        String RecId = xmlStreamReader.getAttributeValue(0);
                        xmlStreamWriter.writeStartElement(localName);
                        xmlStreamWriter.writeAttribute("RecId", RecId);
                    }

                    if (localName.equals("URL")) {
                        String url = xmlStreamReader.getElementText();
                        xmlStreamWriter.writeStartElement(localName);
                        xmlStreamWriter.writeCharacters(url);
                        xmlStreamWriter.writeEndElement();
                    }
                    if (localName.equals("Infomation")) {
                        String news = xmlStreamReader.getElementText();

                        String newsGroup = norPunc(news);


                        xmlStreamWriter.writeStartElement(localName);
                        xmlStreamWriter.writeCharacters(newsGroup);
                        xmlStreamWriter.writeEndElement();
                    }
                    break;
                case XMLStreamConstants.END_ELEMENT:
                    String endLocalName = xmlStreamReader.getLocalName();
                    if (endLocalName.equals("record")) {
                        xmlStreamWriter.writeEndElement();
                    }

                    break;
                case XMLStreamConstants.CHARACTERS:
                    String character = xmlStreamReader.getText();
                    break;

                default:
                    break;
            }
        }
        xmlStreamWriter.writeEndElement();
        xmlStreamReader.close();
        xmlStreamWriter.close();
        fis.close();
        fos.close();
        long endTime = System.currentTimeMillis();
        long time = endTime - startTime;
        System.out.println("Thoi gian chay NorPunc: " + time);

    }

    public String norPuncNor(String news) {
        Pattern PUNC1 = Pattern.compile("((\\.)|(\\?)|(!)|(\\.\\.\\.)|(\\,)|(;)|(:)|(\\)))");
        Pattern PUNC2 = Pattern.compile("\\(");
        Pattern PUNC3 = Pattern.compile("((<=?)|(>=?)|(=))");
        Pattern PUNC4 = Pattern.compile("/");

        String newsCpy = "";
        int start = 0;
        int end = 0;
        String found = "";
        int lastIndex = 0;

        Matcher m = PUNC1.matcher(news);
        while (m.find()) {
            start = m.start();
            end = m.end();
            found = m.group();
            if (start != 0) {
                if (news.charAt(start - 1) == ' ') {
                    start = start - 1;
                }
            }

            if (end != news.length()) {
                if (news.charAt(end) != ' ') {
                    found += " ";
                }
            }
            newsCpy = newsCpy + news.substring(lastIndex, start) + found;
            lastIndex = end;
        }

        if (lastIndex < news.length()) {
            newsCpy += news.substring(lastIndex);
        }

        news = newsCpy;
        newsCpy = "";
        lastIndex = 0;

        m = PUNC2.matcher(news);
        while (m.find()) {
            start = m.start();
            found = m.group();
            end = m.end();
            if (start != 0) {
                if (news.charAt(start - 1) != ' ') {
                    found = " " + found;
                }
            }

            if (end != news.length()) {
                if (news.charAt(end) == ' ') {
                    end = end + 1;
                }
            }

            newsCpy = newsCpy + news.substring(lastIndex, start) + found;
            lastIndex = end;

        }

        if (lastIndex < news.length()) {
            newsCpy += news.substring(lastIndex);
        }


        news = newsCpy;
        newsCpy = "";
        lastIndex = 0;

        m = PUNC3.matcher(news);
        while (m.find()) {
            start = m.start();
            end = m.end();
            found = m.group();

            if (start != 0) {
                if (news.charAt(start - 1) != ' ') {
                    found = " " + found;
                }
            }

            if (end != news.length()) {
                if (news.charAt(end) != ' ') {
                    found = found + " ";

                }
            }

            newsCpy = newsCpy + news.substring(lastIndex, start) + found;
            lastIndex = end;
        }

        if (lastIndex < news.length()) {
            newsCpy += news.substring(lastIndex);
        }


        news = newsCpy;
        newsCpy = "";
        lastIndex = 0;



        m = PUNC4.matcher(news);
        while (m.find()) {
            start = m.start();
            end = m.end();
            found = m.group();

            if (start != 0) {
                if (news.charAt(start - 1) == ' ') {
                    start = start - 1;
                }
            }

            if (end != news.length()) {
                if (news.charAt(end) == ' ') {
                    end = end + 1;
                }
            }


            newsCpy = newsCpy + news.substring(lastIndex, start) + found;
            lastIndex = end;
        }

        if (lastIndex < news.length()) {
            newsCpy += news.substring(lastIndex);
        }

        return newsCpy;
    }

    public String norPunc(String news) {
        Pattern URL_NUM = Pattern.compile("(\\b(https?|ftp|file)://[-A-Z0-9+&@#/%?=~_|!:,.;]*[-A-Z0-9+&@#/%=~_|])|(^www[^ ]*)|([^ ]*.net$)([^ ]*.org$)|([^ ]*.vn$)|([^ ]*.com$)|([^ ]+@[^ ]+)|([\\(\\d][\\d\\.\\- :/\\)]*\\d)");

        System.out.println("package meo; class NorPunc");

        String newsCpy = "";
        int start = 0;
        int end = 0;
        String found = "";
        int lastIndex = 0;
        lastIndex = 0;
        String lastString = "";

        Matcher m = URL_NUM.matcher(news);
        while (m.find()) {
            found = m.group();
            System.out.println(found);
            start = m.start();
            end = m.end();
            found = found.replace(" ", "");
            lastString = news.substring(lastIndex, start);
            lastString = norPuncNor(lastString);
            newsCpy += lastString + found;
            lastIndex = end;
        }
        if (lastIndex < news.length()) {
            lastString = news.substring(lastIndex);
            lastString = norPuncNor(lastString);
            newsCpy += lastString;
        }
        return newsCpy;
    }
}


